import Vue from 'vue'
import VueRouter from 'vue-router'
import OraIndex from '../views/OraIndex.vue'
import OraCart from '../views/OraCart.vue'
import OraOrder from '../views/OraOrder.vue'
import OraHelp from '../views/OraHelp.vue'
import OraLogin from '../views/OraLogin.vue'
import OraMine from '../views/OraMine.vue'
import OraShop from '../views/OraShop.vue'
import OraBuy from '../views/OraBuy.vue'
import OraSearch from '../views/OraSearch.vue'
import OraClass from '../views/OraClass.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: OraIndex,
    children: [
      {
        path: '',
        name: 'index',
        component: OraShop, // 默认子路由组件
      },
      {
        path: 'cart',
        name: 'cart',
        component: OraCart,
        beforeEnter: (to, from, next) => {
          // 检查是否已登录
          const userInfo = localStorage.getItem('userInfo');
          if (userInfo) {
            next(); // 已登录，允许访问
          } else {
            next({ name: 'login', query: { msg: '请先登录' } }); // 未登录，重定向到登录页面
            // 刷新页面
            window.location.reload();
          }
        },
      },
      {
        path: 'order',
        name: 'order',
        component: OraOrder,
        beforeEnter: (to, from, next) => {
          // 检查是否已登录
          const userInfo = localStorage.getItem('userInfo');
          if (userInfo) {
            next(); // 已登录，允许访问
          } else {
            next({ name: 'login', query: { msg: '请先登录' } }); // 未登录，重定向到登录页面
            // 刷新页面
            window.location.reload();
          }
        },
      },
      {
        path: 'help',
        name: 'help',
        component: OraHelp
      },
      {
        path: 'login',
        name: 'login',
        component: OraLogin
      },
      {
        path: 'buy',
        name: 'buy',
        component: OraBuy
      },
      {
        path: 'search',
        name: 'search',
        component: OraSearch
      },
      {
        path: 'class',
        name: 'class',
        component: OraClass
      },
      {
        path: 'mine',
        name: 'mine',
        component: OraMine,
        beforeEnter: (to, from, next) => {
          // 检查是否已登录
          const userInfo = localStorage.getItem('userInfo');
          if (userInfo) {
            next(); // 已登录，允许访问
          } else {
            next({ name: 'login', query: { msg: '请先登录' } }); // 未登录，重定向到登录页面
            // 刷新页面
            window.location.reload();
          }
        },
        children: [
          {
            path: '/mine/index',
            name: 'mineindex',
            component: () => import('@/views/OraMineindex.vue')
          },
          {
            path: '',
            // 重定向
            redirect: { name: 'mineindex' },
          },
          {
            path: 'mineBag',
            name: 'mineBag',
            component: () => import('@/views/OraMineBag.vue')
          },
          {
            path: 'Review',
            name: 'Review',
            component: () => import('@/views/OraReview.vue')
          },
          {
            path: 'account',
            name: 'account',
            component: () => import('@/views/OraAccount.vue')
          },
          {
            path: 'Safe',
            name: 'Safe',
            component: () => import('@/views/OraSafe.vue')
          }
        ]
      }
    ]
  },
]

const router = new VueRouter({
  routes
})

export default router
